<?
include('../controller/handler.php');
include('../controller/twitwords.php');
include('../scaffold/user.php');
include('../scaffold/tag.php');


$user_searched = $_POST['user'];
$tag_searched = $_POST['tag'];
//$tag_searched = $_GET['tag'];

$hd = new Handler();
$tw = new Twitwords();
$hd->openDB();
if($user_searched!=NULL) {
	$quantity = $hd->getAutocompleterResults();
	$offset = 0;
	$users = $tw->searchUser($user_searched, $offset, $quantity);
	$hd->closeDB();

	if(count($users)==0) {
		echo sprintf("<ul><li id=\"error\">No user matching <span id=\"not-found\">%s</span></li></ul>", $user_searched);
	} else {?>
	<ul>
		<?foreach($users as $user) {?>
		<li id="<?echo $user->getUsername();?>">
		<img class="avatar tag" src="<?echo $user->getImage()?>" alt="avatar" />
		<?
		$str = $user->getUsername();
		if(strlen($str)>21) {
			$str = substr($str, 0, 19)."...";
		}
		$str = eregi_replace(
			$user_searched,
			sprintf('<span class="found">\\0</span>', $user_searched),
			$str
		);
		echo $str;
		?>
		<p class="small"><a href="http://twitter.com/<?echo $user->getUsername();?>" target="_blank">
		Twitter page
		</a></p>
		</li>
		<?}?>
		<li id="all"><a href="<?echo $hd->getBaseURL()?>search/user:<?echo $user_searched;?>">View all result</a></li>
	</ul>
		<?
	}
}
if($tag_searched!=NULL) {
	$quantity = $hd->getAutocompleterResults();
	$offset = 0;
	$tags = $tw->searchTag($tag_searched, $offset, $quantity);
	$hd->closeDB();
	
	if(count($tags)==0) {
		echo sprintf("<ul><li id=\"error\">No tag matching <span id=\"not-found\">%s</span></li></ul>", $tag_searched);
	} else {?>
	<ul>
		<?foreach($tags as $tag) {?>
		<li id="<?echo $tag->getUser()->getUsername();?>">
		<img class="avatar tag" src="<?echo $tag->getUser()->getImage()?>" alt="avatar" />
		<?
		$str = $tag->getTag();
		if(strlen($str)>21) {
			$str = substr($str, 0, 19)."...";
		}
		$str = eregi_replace(
			$tag_searched,
			sprintf('<span class="found">\\0</span>', $tag_searched),
			$str
		);
		echo $str;
		?>
		<p class="small"><?echo $hd->deltaToTime(time(), strtotime($tag->getdate()));?></p>
		<p class="small">by <a href="http://twitter.com/<?echo $tag->getUser()->getUsername();?>" target="_blank">
		<?echo $tag->getUser()->getUsername();?>
		</a></p>
		</li>
		<?}?>
		<li id="all"><a href="<?echo $hd->getBaseURL()?>search/tag:<?echo $tag_searched;?>">View all result</a></li>
	</ul>
		<?
	}
}
?>